package codetop.microsoft.T322;

import java.util.Arrays;

/**
 * @Author: 18362
 * @Create: 2022-08-15 11:25:01 星期一
 */
class Solution {
    public int coinChange(int[] coins, int amount) {
        int[] dp = new int[amount+1];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for (int i=0; i<coins.length; ++i) {
            for (int j=0; j<=amount; ++j) {
                if (j >= coins[i] && dp[j-coins[i]] != Integer.MAX_VALUE) {
                    dp[j] = Math.min(dp[j-coins[i]] + 1, dp[j]);
                }
            }
        }
        return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
    }
}